<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>创建短信</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <style>
        .layui-form-label {
            width: 90px;
        }
        .layui-input-block {
            margin-left: 0;
            float: left;
        }
        .location {
            width: 30px;
            padding: 9px 9px 9px 0;
        }
        .layui-form-item {
            display: flex;
        }
        .layui-form {
            width: 490px;
        }
        .layui-input-block {
            flex: 1;
        }
        .form-button {
            margin-top: 20px;
            display: flex;
            justify-content: center;
        }
        .mustNeed {
            margin-left: 10px;
        }
    </style>
    <script>
        layui.use(['form', 'upload', 'element'], function(){
            var form = layui.form;
            var upload = layui.upload;
            var element = layui.element;
            var addParam = { signName: "0617测试签名4",
                ecName: "测试集团",
                cityId: "14",
                countyId: "1401",
                industryType: "1",
                customerType: "1"
            };
            //监听提交
            form.on('submit(formDemo)', function(data){
                //var choose = $("span.layui-upload-choose").text();
                //正则表达式
                var reg = new RegExp("^[A-Za-z0-9\u4e00-\u9fa5]+$");
                //获取输入框中的值
                //判断输入框中有内容
                if(!reg.test($("#signName").val()) || $("#signName").val().length>20){
                    layer.msg("请输入中文、数字或英文！且不超过20位");
                }else {
                    $.ajax({
                        type: 'post',
                        async: false,
                        url: '/signatureManage/judgeSignNameRepeat',
                        data: {
                            "signName": $("#signName").val()
                        },
                        success: function (json) {
                            console.log(json);
                            if(json.data == 0) {
                                $('#upload').trigger("click");
                            }else {
                                layer.msg('签名重复');
                            }
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    });
                    // if (choose != null && choose != '') {
                    //     $.ajax({
                    //         type: 'post',
                    //         async: false,
                    //         url: '/signatureManage/judgeSignNameRepeat',
                    //         data: {
                    //             "signName": $("#signName").val()
                    //         },
                    //         success: function (json) {
                    //             console.log(json);
                    //             if(json.data == 0) {
                    //                 $('#upload').trigger("click");
                    //             }else {
                    //                 layer.msg('签名重复');
                    //             }
                    //         }, error: function () {
                    //             layer.msg('系统异常，请联系管理员处理!');
                    //         }
                    //     });
                    // }else {
                    //     layer.msg('请导入集团授权书!')
                    // }
                }
                return false;
            });
            form.on('submit(reset)', function(data){
                console.log("返回")
            });

            $(document).ready(function () {
                $.ajax({
                    type: 'post',
                    async: false,
                    url: '/util/findUser',
                    success: function (json) {
                        if (json.duty == 1) {
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            form.render();
                        } else if (json.duty == 2) {
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            form.render();
                        }

                    }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
                $("#uploadDiv").css("display","none");
                $("#confirm").css("display","none");
            })


            //根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            };

            //动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="">请先选择地市</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }

            //监听项目  区县
            form.on('select(cityId)', function (data) {
                var data2 = selectCountyId(data.value);
                appendCounty(data2);
            });
            upload.render({
                elem : '#import',
                url : '/signatureManage/createSignature',
                auto : false, //关闭自动上传
                accept : 'png|jpg', //普通文件
                exts : 'png|jpg', //只允许上传pdf文件 //,multiple: true
                bindAction : '#upload',
                async: false,
                before : function(event) {
                    this.data = {
                        signName: $("#signName").val(),
                        ecName: $("#group").val(),
                        cityId: $("#cityId").val(),
                        countyId: $("#countyId").val(),
                        industryType: $("#business").val(),
                        customerType: $("#clientType").val()
                    };
                    console.log(this.data);
                    layer.load(); //上传loading
                },
                done : function(obj) {
                    layer.closeAll('loading'); //关闭loading
                    if (obj.retCode == 0) {
                        layer.confirm('签名新增成功，确认返回签名查询页面', {
                            btn : [ '确认', '取消' ] //按钮
                        }, function() {
                            location.href = "signatureTable.html";
                        }, function() {
                            location.href = "signatureTable.html";
                        });
                    } else {
                        layer.msg(obj.msg)
                    }
                },
                error : function(index, upload) {
                    layer.closeAll('loading'); //关闭loading
                    layer.msg("上传过程中出现异常，请重试");
                }
            });


            $("#needUpload").click(function(){
                var needUpload = $("input[name='needUpload']:checked").val();
                if(needUpload==1){
                    $("#uploadDiv").css("display","");
                    $("#confirm").css("display","");
                    $("#submit").css("display","none");
                }else if(needUpload==2){
                    $("#uploadDiv").css("display","none");
                    $("#confirm").css("display","none");
                    $("#submit").css("display","");
                }
            });

            $("#submit").click(function(){

                var param = {
                    signName: $("#signName").val(),
                    ecName: $("#group").val(),
                    cityId: $("#cityId").val(),
                    countyId: $("#countyId").val(),
                    industryType: $("#business").val(),
                    customerType: $("#clientType").val()
                };
                var url='/signatureManage/createSignature2';
                layer.confirm('确认签名信息无误吗？是否需要再次查看', {
                    btn : [ '不需要', '需要' ] //按钮
                }, function() {
                    var index = layer.load(1, {shade: [0.1,'#fff'] });
                    $.ajax({
                        type: 'post',
                        data: param,
                        url: url,
                        async: false,
                        success: function (obj) {
                            layer.close(index);
                            if (obj.retCode == 0) {
                                layer.confirm('签名新增成功，确认返回签名查询页面', {
                                    btn : [ '确认', '取消' ] //按钮
                                }, function() {
                                    location.href = "signatureTable.html";
                                }, function() {
                                    location.href = "signatureTable.html";
                                });
                            } else {
                                layer.msg(obj.msg)
                            }
                        },
                        error: function(){
                            layer.close(index);
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    });
                });
            })



        });
        function back() {
            location.href = "signatureTable.html";
        }
    </script>
</head>
<body>
<div class="layui-content-body">
    <div class="scroll-box">
        <div class="breadcrumb-box">
            <span class="layui-breadcrumb" lay-separator="|"> <a
                    href="javascript:returnIndex2()">首页&nbsp;</a>
                <a href="signatureTable.html">&nbsp;签名管理&nbsp;</a>
                <a href=""> &nbsp;签名新增&nbsp; </a>
                </span>
            </span>
        </div>
        <div class="content-box">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">归属地:</label>
                    <div class="layui-inline">
                        <label class="layui-form-label location">地市</label>
                        <div class="layui-input-block" style="width:110px">
                            <select name="cityId" lay-filter="cityId" id="cityId" required  lay-verify="required">
                                <option value="">请选择</option>
                                <option value="11">苏州</option>
                                <option value="13">宿迁</option>
                                <option value="14">南京</option>
                                <option value="15">连云港</option>
                                <option value="16">徐州</option>
                                <option value="17">常州</option>
                                <option value="18">镇江</option>
                                <option value="19">无锡</option>
                                <option value="20">南通</option>
                                <option value="21">泰州</option>
                                <option value="22">盐城</option>
                                <option value="23">扬州</option>
                                <option value="25">省级</option>
                            </select>
                        </div>
                        <div class="layui-inline mustNeed"><span style="color:red;">*</span></div>
                    </div>
                    <div class="layui-inline countyId">
                        <label class="layui-form-label location">区县</label>
                        <div class="layui-input-block" style="width:110px">
                            <select name="countyId" lay-filter="countyId" id="countyId" >
                                <option value="">全部</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">签名名称:</label>
                    <div class="layui-input-block">
                        <input type="text" name="sign" required  lay-verify="required" placeholder="请输入15字以内中英文或数字签名" autocomplete="off" class="layui-input" id="signName">
                    </div>
                    <div class="layui-inline mustNeed"><span style="color:red;">*</span></div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">归属集团名称:</label>
                    <div class="layui-input-block">
                        <input type="text" name="group" required  lay-verify="required" placeholder="请输入签名归属集团名称" autocomplete="off" class="layui-input" id="group">
                    </div>
                    <div class="layui-inline mustNeed"><span style="color:red;">*</span></div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">行业属性:</label>
                    <div class="layui-input-block">
                        <select name="business" lay-verify="required" id="business">
                            <option value=""></option>
                            <option value="1">农、林、牧、渔</option>
                            <option value="2">采矿业</option>
                            <option value="3">制造业</option>
                            <option value="4">电力、燃气及水的生产和供应业</option>
                            <option value="5">建筑业</option>
                            <option value="6">交通运输、仓储和邮政业</option>
                            <option value="7">信息传输、计算机服务和软件业</option>
                            <option value="8">批发和零售业</option>
                            <option value="9">住宿和餐饮业</option>
                            <option value="10">金融业</option>
                            <option value="11">房地产业</option>
                            <option value="12">租赁和商务服务业</option>
                            <option value="13">科学研究、技术服务和地质勘查业</option>
                            <option value="14">水利、环境和公共设施管理业</option>
                            <option value="15">居民服务和其他服务业</option>
                            <option value="16">教育</option>
                            <option value="17">卫生、社会保障和社会福利业</option>
                            <option value="18">文化、体育和娱乐业</option>
                            <option value="19">公共管理和社会组织</option>
                            <option value="20">国际组织</option>
                            <option value="21">其他</option>
                        </select>
                    </div>
                    <div class="layui-inline mustNeed"><span style="color:red;">*</span></div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">客户分类:</label>
                    <div class="layui-input-block">
                        <select name="clientType" lay-verify="required" id="clientType">
                            <option value=""></option>
                            <option value="1">党政军</option>
                            <option value="2">民生</option>
                            <option value="3">金融</option>
                            <option value="4">物流</option>
                            <option value="5">游戏</option>
                            <option value="6">电商</option>
                            <option value="7">微商（个人）</option>
                            <option value="8">沿街商铺（中小）</option>
                            <option value="9">企业（大型）</option>
                            <option value="10">教育培训</option>
                            <option value="11">房地产</option>
                            <option value="12">医疗器械、药店</option>
                            <option value="13">其他</option>
                        </select>
                    </div>
                    <div class="layui-inline mustNeed"><span style="color:red;">*</span></div>
                </div>
                <div class="layui-form-item" style="color: red">
                    &nbsp;&nbsp;&nbsp;若签名与归属集团保持一致则不需要签名授权书
                </div>
                <div class="layui-form-item" style="color: red">
                    &nbsp;&nbsp;&nbsp;例如&nbsp;签名：测试签名，集团：测试签名有限公司
                </div>
                <div class="layui-form-item" id = "needUpload">
                    <label class="layui-form-label" style="width:100px">是否上传授权书</label>
                    <div class="layui-input-block">
                        <input class="radioItem" lay-filter="gis" type="radio" name="needUpload" title="需要"  value="1">
                        <input class="radioItem" lay-filter="gis" type="radio" name="needUpload" title="不需要" value="2" checked="checked">
                    </div>
                </div>
                <div class="layui-form-item" style="display: flex;" id="uploadDiv">
                    <label class="layui-form-label">签名授权书:</label>
                    <button type="button" class="layui-btn layui-btn-normal" id="import">
                        <i class="layui-icon">&#xe67c;</i>导入
                    </button>
                    <div class="layui-inline mustNeed"><span style="color:red;">*</span></div>
                </div>
                <div class="layui-form-item" style="display:none;">
                    <button class="layui-btn" id="upload" type="button">提交</button>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block form-button">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo" id="confirm" type="button">确定</button>
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo" id="submit" type="button" >确定</button>
                        <button type="reset" class="layui-btn layui-btn-normal" onclick="back()">返回</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>